package org.pf.sample.controllers
{
	import org.pf.base.pfBlocks.PFController;
	import org.pf.interfaces.IPFController;
	import Box2D.Dynamics.b2Body;
	import org.pf.sample.events.CamControllerEvent;
	
	/**
	 * ...
	 * @author zeksa'
	 */
	public class SimpleCamController extends PFController implements IPFController
	{
		public static const NAME:String = "simpleCamController";
		
		public function SimpleCamController()
		{
			super(NAME);
		}
		
		override public function activate():void
		{
			systemCache.layers.controlLayer.addEventListener(CamControllerEvent.STARTED_SIMPLE_CONTROL, attachSimpleCamera);
			systemCache.layers.controlLayer.addEventListener(CamControllerEvent.FINISHED_SIMPLE_CONTROL, disattachSimpleCamera);
		}
		
		override public function deactivate():void
		{
			systemCache.layers.controlLayer.removeEventListener(CamControllerEvent.STARTED_SIMPLE_CONTROL, attachSimpleCamera);
			systemCache.layers.controlLayer.removeEventListener(CamControllerEvent.FINISHED_SIMPLE_CONTROL, disattachSimpleCamera);
		}
		
		private function attachSimpleCamera(e:CamControllerEvent):void
		{
			var body:b2Body;
			for (var b:b2Body = _prince.get_SystemCache().currentWorld.GetBodyList(); b; b = b.GetNext())
			{
				if (b.GetUserData() && b.GetUserData().bodyName == e.bodyName)
				{
					body = b;
					break;
				}
			}		
			body.AttachCamera();
		}
		
		private function disattachSimpleCamera(e:CamControllerEvent):void
		{
			var body:b2Body;
			for (var b:b2Body = _prince.get_SystemCache().currentWorld.GetBodyList(); b; b = b.GetNext())
			{
				if (b.GetUserData() && b.GetUserData().bodyName == e.bodyName)
				{
					body = b;
					break;
				}
			}
			body.DisattachCamera();
		}
	
	}

}